﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;
using System.Net.Sockets;
using System.Threading;

namespace MasterAPI
{
    public class MasterServerConn
    {
        private string data = null;
        private string ipStr = "";
        private int threads = 1;
        public static ArrayList clientSockets = new ArrayList();
        public static ArrayList clientThreads = new ArrayList();
        public static ArrayList clientClasses = new ArrayList();
        public string serverPassword = "";
        public static Dictionary<IPEndPoint, GameServerObject> gameServers = new Dictionary<IPEndPoint, GameServerObject>();
        public static int activeClients = 0;
        public static int availableClients = 0;
        public void StartMasterServer()
        {
            //Set Start Title
            Console.Title = "Todesesser Master Server - Online Servers: " + activeClients + ", Available Servers: " + availableClients;
            //Data buffer for incoming data.
            byte[] bytes = new Byte[1024];

            //Create LocalEndpoint to Listen on.
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipv4Address = new IPAddress(new byte[4] { 0, 0, 0, 0 });
            foreach (IPAddress address in hostEntry.AddressList)
            {
                string addr = address.ToString();
                if (addr.Contains(":") == false)
                {
                    ipv4Address = address;
                    ipStr = addr;
                }
            }
            IPEndPoint localEndPoint = new IPEndPoint(ipv4Address, 5050);

            //DataArray
            ArrayList dataLines = new ArrayList();

            //Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            //Try and Listen
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);
                while (true)
                {
                    SpecialConsole.Text.WriteLine("Waiting for Connection on " + ipv4Address + ":" + localEndPoint.Port + "...", ConsoleColor.Red);
                    data = null;
                    Socket gameServerConn = listener.Accept();
                    GameServerConnThread tempGameServer = new GameServerConnThread(gameServerConn, listener, this.threads, dataLines, clientSockets, Encoding.ASCII.GetBytes(serverPassword));
                    clientSockets.Add(gameServerConn);
                    Thread tempGameServerThread = new Thread(new ThreadStart(tempGameServer.main));
                    clientClasses.Add(tempGameServer);
                    tempGameServerThread.Start();
                    clientThreads.Add(tempGameServerThread);

                    Console.Title = "Todesesser Master Server - Active Servers: " + clientSockets.Count;
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}
